package com.yw.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.yw.dataAsset.domain.DataAssetCatalogue;
import com.yw.dataAsset.domain.DataAssetFieldInfo;
import com.yw.dataAsset.domain.DataAssetInfo;
import com.yw.dataAsset.dto.AddDataAssetDTO;
import com.yw.dataAsset.dto.DataAssetDTO;
import com.yw.dataAsset.vo.AssetFieldVO;
import com.yw.dataAsset.vo.AssetVO;
import com.yw.dataAsset.vo.MyDataAssetVO;
import com.yw.dataStandardCatalog.domain.DataStandardCatalog;
import com.yw.mapper.DataAssetMapper;
import com.yw.piliangDTO.StatusDTO;
import com.yw.service.*;
import com.yw.utils.BeanConverter;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataAssetServiceImpl extends ServiceImpl<DataAssetMapper, DataAssetInfo>
                                      implements DataAssetService {

    @Resource
    private DataAssetFieldService dataAssetFieldService;

@Resource
private DirectoryService directoryService;

    @Resource
    private DataAssetMapper dataAssetMapper;

    @Resource
    private DataAssetCatalogueService dataAssetCatalogueService;

    @Resource
    private DataStandardCatalogService dataStandardCatalogService;



    @Override
    public Page<MyDataAssetVO> pageAllByQuery(DataAssetDTO dataAssetDTO) {
        Page<DataAssetInfo> page = new Page<>(dataAssetDTO.getPageNumber(), dataAssetDTO.getPageSize());
        LambdaQueryWrapper<DataAssetInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dataAssetDTO.getStaticStatus() !=null,DataAssetInfo::getStaticStatus,dataAssetDTO.getStaticStatus())
                .like(dataAssetDTO.getDatabaseCname()!=null,DataAssetInfo::getDatabaseCname,dataAssetDTO.getDatabaseCname())
                .like(dataAssetDTO.getDatabaseEname() !=null,DataAssetInfo::getDatabaseEname,dataAssetDTO.getDatabaseEname());
        Page<DataAssetInfo> pageResult = this.page(page,wrapper);
        Page<MyDataAssetVO> returnPage = new Page<>();
        returnPage.setRecords(BeanConverter.convertList(pageResult.getRecords(), MyDataAssetVO.class));
        returnPage.setCurrent(pageResult.getCurrent());
        returnPage.setTotal(pageResult.getTotal());
        returnPage.setSize(pageResult.getSize());
        return returnPage;
    }

    @Override
    public AssetVO onclickName(Integer id){
        DataAssetInfo one = this.getById(id);
        AssetVO assetVO = new AssetVO();
        assetVO.setId(id);
        assetVO.setDatabaseCname(one.getDatabaseCname());
        assetVO.setDatabaseEname(one.getDatabaseEname());
        assetVO.setDatabaseDesc(one.getDatabaseDesc());
        LambdaQueryWrapper<DataAssetCatalogue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataAssetCatalogue::getAssetId,id);
        List<DataAssetCatalogue> list = dataAssetCatalogueService.list(wrapper);
        ArrayList<String> assetCalagueName = new ArrayList<>();
        for (DataAssetCatalogue dataAssetCatalogue : list) {
            assetCalagueName.add(dataAssetCatalogue.getName());
        }
        assetVO.setAssetCalagueName(assetCalagueName);
        LambdaQueryWrapper<DataAssetFieldInfo> fieldWrapper = new LambdaQueryWrapper<>();
        fieldWrapper.eq(DataAssetFieldInfo::getDatabaseId,id);
        List<DataAssetFieldInfo> Flist = dataAssetFieldService.list(fieldWrapper);
        ArrayList<AssetFieldVO> assetFieldVOS = new ArrayList<>();
        for (DataAssetFieldInfo dataAssetFieldInfo : Flist) {
            AssetFieldVO assetFieldVO = new AssetFieldVO();
            assetFieldVO.setId(dataAssetFieldInfo.getId());
            assetFieldVO.setFieldCname(dataAssetFieldInfo.getFieldCname());
            assetFieldVO.setFieldDesc(dataAssetFieldInfo.getFieldDesc());
            assetFieldVO.setFieldEname(dataAssetFieldInfo.getFieldEname());
            DataStandardCatalog oneCatalog = dataStandardCatalogService.getById(dataAssetFieldInfo.getStandardId());
            assetFieldVO.setLength(oneCatalog.getLength());
            assetFieldVO.setNumAccuracy(oneCatalog.getNumAccuracy());
            assetFieldVO.setDefaultNum(oneCatalog.getDefaultNum());
            assetFieldVO.setValueRange(oneCatalog.getValueRange());
            assetFieldVO.setCode(oneCatalog.getCode());
            assetFieldVOS.add(assetFieldVO);
        }
        assetVO.setDatabaseFields(assetFieldVOS);
        return assetVO;
    }

    @Override
    public Boolean addDataAsset(AddDataAssetDTO addDataAssetDTO) {
        DataAssetInfo dataAssetInfo = new DataAssetInfo();
        dataAssetInfo.setDatabaseCname(addDataAssetDTO.getDatabaseCname());
        dataAssetInfo.setDatabaseEname(addDataAssetDTO.getDatabaseEname());
        dataAssetInfo.setDatabaseDesc(addDataAssetDTO.getDatabaseDesc());
        dataAssetInfo.setCreateTime(LocalDateTime.now());
        dataAssetInfo.setUpdateTime(LocalDateTime.now());
        dataAssetInfo.setStaticStatus(0);
        Boolean a = this.save(dataAssetInfo);
        List<DataAssetCatalogue> dataAssetCatalogues = new ArrayList<>();
        for (Integer databaseId : addDataAssetDTO.getDatabaseIds()) {
            DataAssetCatalogue dataAssetCatalogue = new DataAssetCatalogue();
            dataAssetCatalogue.setTotalId(databaseId);
            dataAssetCatalogue.setAssetId(dataAssetInfo.getId());
            dataAssetCatalogue.setName("庹艾家");
            dataAssetCatalogues.add(dataAssetCatalogue);
        }
        boolean c = dataAssetCatalogueService.saveBatch(dataAssetCatalogues);
        List<DataAssetFieldInfo> databaseFields = addDataAssetDTO.getDatabaseFields();
        for (DataAssetFieldInfo databaseField : databaseFields) {
            databaseField.setDatabaseId(dataAssetInfo.getId());
        }
        boolean b = dataAssetFieldService.saveBatch(databaseFields);

        if (a && b && c) {
           return true;
       }else {
           return false;
       }


    }


    //批量发布或停用
    @Override
    public Boolean batch(StatusDTO statusDTO) {
        return dataAssetMapper.batch(statusDTO);
    }

    @Override
    public Boolean updateStatic(Integer id) {
        DataAssetInfo dataAssetInfo = dataAssetMapper.selectById(id);
        switch (dataAssetInfo.getStaticStatus()){
            case 0, 2:
                dataAssetInfo.setStaticStatus(1);break;
            case 1:
                dataAssetInfo.setStaticStatus(2);break;
        }
        if(dataAssetMapper.updateById(dataAssetInfo) !=0){
            return true;
        }else {
            return false;
        }
    }
//回显
    @Override
    public AddDataAssetDTO getOne(Integer id) {
        return null;
    }

    @Override
    public Boolean updateDataAsset(Integer id,AddDataAssetDTO addDataAssetDTO) {
        DataAssetInfo one = this.getById(id);
        DataAssetInfo dataAssetInfo = new DataAssetInfo();
        dataAssetInfo.setDatabaseCname(addDataAssetDTO.getDatabaseCname());
        dataAssetInfo.setDatabaseEname(addDataAssetDTO.getDatabaseEname());
        dataAssetInfo.setDatabaseDesc(addDataAssetDTO.getDatabaseDesc());
        dataAssetInfo.setId(id);
        dataAssetInfo.setCreateTime(one.getCreateTime());
        dataAssetInfo.setUpdateTime(LocalDateTime.now());
        dataAssetInfo.setStaticStatus(0);
        this.updateById(dataAssetInfo);
        LambdaQueryWrapper<DataAssetCatalogue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataAssetCatalogue::getAssetId,id);
        dataAssetCatalogueService.remove(wrapper);
        List<DataAssetCatalogue> dataAssetCatalogues = new ArrayList<>();
        for (Integer databaseId : addDataAssetDTO.getDatabaseIds()) {
            DataAssetCatalogue dataAssetCatalogue = new DataAssetCatalogue();
            dataAssetCatalogue.setTotalId(databaseId);
            dataAssetCatalogue.setAssetId(id);
            dataAssetCatalogue.setName(directoryService.getPathById(databaseId));
            dataAssetCatalogues.add(dataAssetCatalogue);
        }
        boolean c = dataAssetCatalogueService.saveBatch(dataAssetCatalogues);

        LambdaQueryWrapper<DataAssetFieldInfo> dataAssetWrapper = new LambdaQueryWrapper<>();
        dataAssetWrapper.eq(DataAssetFieldInfo::getDatabaseId,id);
        dataAssetFieldService.remove(dataAssetWrapper);
        List<DataAssetFieldInfo> databaseFields = addDataAssetDTO.getDatabaseFields();
        for (DataAssetFieldInfo databaseField : databaseFields) {
            databaseField.setDatabaseId(id);
        }
        boolean b = dataAssetFieldService.saveBatch(databaseFields);

        if ( b && c) {
            return true;
        }else {
            return false;
        }
    }


}
